package com.cema.manage.mail;

import com.cema.manage.project.enums.RedisEnum;
import com.cema.manage.redis.RedisService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration;
import org.springframework.boot.autoconfigure.session.SessionProperties;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Properties;

/**
 * @author Administrator
 */
@Service
public class MailServiceImpl implements MailService {

    @Value("${spring.mail.username}")
    private String from;

    @Resource
    private JavaMailSender mailSender;

    @Resource
    private RedisService redisService;

    // 发送简单邮件
    @Override
    @Async("asyncServiceExecutor")
    public void sendSimpleMail(String subject, String content) {
        try {
            JavaMailSenderImpl mailSenderImpl = new JavaMailSenderImpl();
            mailSenderImpl.setHost((String) redisService.get(RedisEnum.MAIL_SMTP_HOST.getKey()));
            mailSenderImpl.setPort(465);
            mailSenderImpl.setUsername((String) redisService.get(RedisEnum.MAIL_SMTP_USERNAME.getKey()));
            mailSenderImpl.setPassword((String) redisService.get(RedisEnum.MAIL_SMTP_PASSWORD.getKey()));
            Properties properties = mailSenderImpl.getJavaMailProperties();
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory"); // 使用JSSE的SSL
            // socketfactory来取代默认的socketfactory
            properties.put("mail.smtp.socketFactory.fallback", "false"); // 只处理SSL的连接,对于非SSL的连接不做处理
            properties.put("mail.smtp.ssl.enable", true);
            Session session = Session.getInstance(properties);
            mailSenderImpl.setSession(session);
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom((String) redisService.get(RedisEnum.MAIL_FROM.getKey()));
            String mail = (String) redisService.get(RedisEnum.YOUR_MAIL.getKey());
            message.setTo(mail.split(","));
            message.setSubject(subject);
            message.setText(content);
            mailSenderImpl.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 发送带附件的邮件
    @Override
    public void sendAttachFileMail(String to, String subject, String content, File file) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
            //邮件发送人
            messageHelper.setFrom(from);
            //邮件接收人
            messageHelper.setTo(to);
            //邮件主题
            message.setSubject(subject);
            //邮件内容
            messageHelper.setText(content);
            //添加附件
            messageHelper.addAttachment(file.getName(), file);
            //发送
            mailSender.send(message);

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    // 发送HTML格式文件
    @Override
    public void sendHtmlMail(String to, String subject, String content) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
            //邮件发送人
            messageHelper.setFrom(from);
            //邮件接收人
            messageHelper.setTo(to);
            //邮件主题
            message.setSubject(subject);
            //邮件内容
            messageHelper.setText(content, true);
            //发送
            mailSender.send(message);

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }


}
